#!/bin/bash
#开始界面
echo -e "\E[31m================================= \E[0m"
echo -e "\E[31m 首页 \E[0m"
echo -e "\E[34m 1:创建用户 \E[0m"
echo -e "\E[33m 2:用户登录 \E[0m"
echo -e "\E[36m 3:查看排行榜 \E[0m"
echo -e "\E[32m 4:修改密码 \E[0m"
echo -e "\E[33m 0:退出 \E[0m"
echo -e "\E[31m 输入其他无效 \E[0m"
echo -e "\E[31m================================= \E[0m"
read -p "请输入你的选项：" wj
#创建用户
if [ $wj == 1 ]
then
	[ ! -e wjmd ]&&touch wjmd&&echo "玩家：密码">> wjmd
	echo -e "\E[33m 开始创建用户 \E[0m"
	read -p "请输入用户名：" name
	cut -d ":" -f 1 wjmd | grep "\<$name\>" >> /dev/null 2>&1
	sousuo=`echo $?`
	if [ $sousuo -ne 0 ]
	then 
		read -p "请输入密码：" mima1
		read -p "确认密码：" mima2
		if [ $mima1 == $mima2 ]
		then
			echo -e "\E[32m 创建成功 \E[0m"
		else
			echo -e "\E[31m 密码不一致，回退至首页 \E[0m"
		fi
	else
		echo -e "\E[31m 用户名已存在 \E[0m"
	fi
	echo "$name:$mima1" >>  wjmd
	bash zhiyong.sh
#登陆界面
elif [ $wj == 2 ]
then
	read -p "输入用户名" name
	grep "\<$name\>" wjmd >> /dev/null 2>&1
	if [ $? -eq 0 ]
	then 
		pswd=`grep "\<$name\>" wjmd | cut -d ":" -f 2`
		for ((i=1;i<=3;i++))
		do
			read -p "请输入密码：" mm
			if [ $mm == $pswd ]
			then
				echo -e "\E[32m 密码正确，进入游戏\E[0m"
				#q="/root/mc"
                		[ ! -e /tmp/mc ]&&touch /tmp/mc && echo "$mane"> /tmp/mc
				echo "$name"> /tmp/mc
			        #if [ ! -f $q ];then
				#	touch $q
				#else
				#	echo "$name">$q
				#fi					
				break
			else
				echo -e "\E[31m 密码错误 \E[0m"
			fi
			if [ $i == 3 ]
			then
				echo -e "\E[31m 密码输入多次错误，退出游戏 \E[0m"
exit
			fi
		done	

	else 
		echo -e "\E[31m 用户名不存在,回退至首页\E[0m"
		exit
	fi
#游戏
python3 ./youxi_main.py
bash zhiyong.sh
#排行榜
elif [ $wj == 3 ]
then
	echo "`cat /tmp/mc`:`cat /tmp/xie` " >> /tmp/phb
	#cat /tmp/xie
	sort -n -r /tmp/phb
	bash zhiyong.sh
#修改密码
elif [ $wj == 4 ]
then
	echo -e "\E[31m 修改密码 \E[0m"
	read -p "请输入用户名：" nam
	cut -d ":" -f 1 wjmd | grep "\<$nam\>" >> /dev/null 2>&1
	ss=`echo $?`
	if [ $ss -eq 0 ]
	then
		read -p "请输入原密码：" ma
		paw=`grep "\<$nam\>" wjmd | cut -d ":" -f 2`
		if [ $ma == $paw ]
		then
			read -p "请输入新密码：" mi1
			read -p "确认密码：" mi2
			if [ $mi1 == $mi2 ]
			then
				tt=$PWD/wjmd
				[ ! -e xiugaimima.sh ] && touch xiugaimima.sh
				echo "#!/bin/bash" > xiugaimima.sh
				echo "sed -i 's/$nam:$paw/$nam:$mi1/g' $tt" >> xiugaimima.sh
				source xiugaimima.sh
				rm -rf xiugaimima.sh
			else
				echo -e "\E[31m 密码不一致 \E[0m"
			fi
		else
			echo -e "\E[31m 密码错误 \E[0m"
		fi
	else
		echo -e "\E[31m 用户名不存在 \E[0m"
	fi
	bash zhiyong.sh
#退出
elif [ $wj == 0 ]
then
	exit
else
	echo -e "\E[31m 输入错误,重新输入 \E[0m"
bash zhiyong.sh
fi


